package com.wavecoler.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.persistence.Entity;

import com.google.appengine.api.datastore.KeyFactory;

public class Utility {

public static boolean doesEntityExist(Class<? extends Entity> entityClass, String id) {
	PersistenceManager pm = null;
	try {
		pm = PMF.get().getPersistenceManager();
		String idKeyString = KeyFactory.createKeyString(entityClass.getSimpleName(), id);
		Query q = pm.newQuery("select encodedKey from " + entityClass.getName()
						   + " where encodedKey = '" +	idKeyString + "'");
	    List<?> keys = (List<?>)q.execute();
	    return keys.size() > 0;
	} catch (Exception e) {
	    throw new RuntimeException(e);
	} finally {
	     if (pm != null) pm.close();
	  }
	}
}
